import getApiData, { getApisData } from "@/services/cpasRequest";


export const mergeList = (arr1: any[], arr2: any[]) => {
    // 对2个待合并的数组进行快速排序
    arr1.sort((a, b) => a.XH - b.XH);
    arr2.sort((a, b) => a.XH - b.XH);

    const result: any = [];
    let i = 0, j = 0, pre = '';

    // 双指针扫描两个有序数组，并将其合并到 result 数组中
    while (i < arr1.length || j < arr2.length) {
        let index: any = '';
        if (i >= arr1.length || +arr1[i].GUID > +arr2[j].GUID) {
            index = arr2[j].GUID;
            j++;
        } else {
            index = arr1[i].GUID;
            i++;
        }
        if (index !== pre) {
            pre = index;
            result.push(index);
        }
    }
    return result;
}



export async function DelZBAdjustBatch(dbname: string, billID: string, paramsData: any[]) {

    const postData = {
        apis: [
            {
                name: 'sqlApi.update',
                params: { name: `[${dbname}].[dbo].[tmp_${billID}]`, rows: paramsData }
            },
        ],
        reload: true,
        params: {}
    }
    const resApis = await getApisData(postData);
    const resUpdate = resApis.data['sqlApi.update']
    if (resApis.ok && resUpdate.ok) {
        return Promise.resolve({
            data: `[${dbname}]..[tmp_${billID}], 批量操作项目成功！`,
            success: true,
        });
    }
    return Promise.resolve({
        data: resApis.data,
        success: false,
    });
}
// 删除分录
export async function detailDel(whereFrom: string, params: {
    dbname: string;
    billID: string;
    GUID: string;
}) {

    let requestName: any = '';
    if (whereFrom === '账表调整') {
        requestName = 'account.ZBAdjust.detail.del';
    } else {
        requestName = 'account.auditAdjust.detail.del';
    }
    const res = await getApiData(requestName, params);
    if (res.ok) return res.ok;
};


// 保存单据编辑
export async function ZBAdjustBill(whereFrom: string, params: {
    dbname: string,
    billID: string,
    Data_ID: string,
    billNo: string,
    TZLX: number | string,
    TZQJ: string,
    isTZ: string,
    remarks: string,
    editUser: string | undefined
    isRetrospect?: string | boolean,
    adjustType?: string | number
} & any) {

    let requestName: any = '';
    if (whereFrom === '账表调整') {
        requestName = 'account.ZBAdjust.bill.save';
    } else {
        requestName = 'account.auditAdjust.bill.save';
    }
    const res = await getApiData(requestName, params);
    if (res.ok) {
        return Promise.resolve({
            data: res.data,
            success: true,
        });
    }
    return Promise.resolve({
        data: res.data,
        success: false,
    });
};

// 获取审计调整新单据编号
export async function getReportItemsDatas(whereFrom: string, params: {
    dbname: string;
    Data_ID: string;
    userCode: string | undefined
}) {

    let requestName: any = '';
    if (whereFrom === '账表调整') {
        requestName = 'account.ZBAdujst.getNewBillNo';
    } else {
        requestName = 'account.auditAdjust.getNewBillNo';
    }

    const res = await getApiData(requestName, params);
    if (res.ok) {
        return Promise.resolve({
            data: res.data,
            success: true,
        });
    }
    return Promise.resolve({
        data: [],
        success: false,
    });
};


// 获取审计调整新单据编号
export async function itemsDatasEditXH(whereFrom: string, params: {
    dbname: string;
    billID: string;
    rowIndex: number;
    rowGuid: string;
    toIndex: number;
    toRowGuid: string;
}) {

    let requestName: any = '';
    if (whereFrom === '账表调整') {
        requestName = 'account.ZBAdjust.detail.editXH';
    } else {
        requestName = 'account.auditAdjust.detail.editXH';
    }

    const res = await getApiData(requestName, params);
    if (res.ok) {
        return Promise.resolve({
            data: res.data,
            success: true,
        });
    }
    return Promise.resolve({
        data: [],
        success: false,
    });
};

// account.ZBAdjust.detail.editXH


// 获取左侧切换的数据
export async function getSubjectType(params: {
    dbname: string;
    Data_ID: string
}) {
    const res = await getApiData("account.subject.getType", params);
    return Promise.resolve({
        data: res.data,
        success: res.ok,
    });
}

// 获取左侧树数据
export async function getDataListEx(params: {
    dbname: string;
    Data_ID: string;
    isHasFZZ?: boolean;
}) {
    // account.subject.getDataList
    const res = await getApiData("account.subject.getDataListEx", params);
    return Promise.resolve({
        data: res.data,
        success: res.ok,
    });
}


// 重算单据
export async function reCaljustBill(params: {
    Data_ID: string,
    dbname: string,
    editUser: string | any,
    isRecalTZ: boolean;
}) {
    const res = await getApiData('account.reportItem.reCal', params);
    if (res.ok) {
        return Promise.resolve({
            data: res.data,
            success: true,
        });
    }
    return Promise.resolve({
        data: res.data,
        success: false,
    });
};





// 取消单据编辑
export async function ZBAdjustCancel(whereFrom: string, params: {
    dbname: string,
    billID: string,
    Data_ID: string,
    editUser: string | undefined
}) {

    let requestName: any = '';
    if (whereFrom === '账表调整') {
        requestName = 'account.ZBAdjust.bill.cancel';
    } else {
        requestName = 'account.auditAdjust.bill.cancel';
    }
    const res = await getApiData(requestName, params);
    if (res.ok) return res.ok;
};

// 清理调整分录明细编辑过程中生成的临时表
export async function AdjustingEntryClearTempTable(params: {
    dbname: string,
    billID: string,
}) {
    const res = await getApiData("account.adjustingEntry.clearTempTable", params);
    if (res.ok) return res.ok;
};

// 设置自动结转分录
export async function subjectGetJZKM(params: {
    dbname: string,
    Data_ID: string,
}) {
    const res = await getApiData("account.subject.getJZKM", params);
    if (res.ok) return res.data;
};

// 设置结转科目
export async function updateJZKM(params: {
    dbname: string,
    Data_ID: string,
    editUser: string | undefined,
    QCJZKM: string,
    QMJZKM: string,
    YQSYKM: string
}) {
    const res = await getApiData("account.subject.updateJZKM", params);
    if (res.ok) return res.ok;
};

// 获取指定科目的账龄设置
export async function getZLZone(params: {
    dbname: string,
    KMMC: string,
}) {
    const res = await getApiData("account.subject.getZLZone", params);
    if (res.ok) {
        return Promise.resolve({
            data: res.data,
            success: true,
        });
    }
    return Promise.resolve({
        data: [],
        success: false,
    });
};
// 更新账龄
export async function updateZL(whereFrom: string, params: {
    dbname: string,
    billID: string,
    GUID: string,
    M1: string | number,
    M2: string | number,
    M3: string | number,
    M4: string | number,
    M5: string | number,
    M6: string | number,
    M7: string | number,
    M8: string | number,
    M9: string | number,
    M10: string | number,
    M11: string | number,
    M12: string | number,
    Y1: string | number,
    Y2: string | number,
    Y3: string | number,
    Y4: string | number,
    Y5: string | number,
    Y6: string | number,
    Y7: string | number,
    Y8: string | number,
    Y9: string | number,
    Y10: string | number
}) {
    let requestName: any = '';
    if (whereFrom === '账表调整') {
        requestName = 'account.ZBAdjust.detail.updateZL';
    } else {
        requestName = 'account.auditAdjust.detail.updateZL';
    };
    const res = await getApiData(requestName, params);
    if (res.ok) return res.ok;
};




export async function ZBAdjustEditHCFSE(params: {
    dbname: string,
    billID: string,
    isEditHCFSE: boolean;
    GUID: string,
    isHCFSE: string,
}) {
    const res = await getApiData('account.ZBAdjust.detail.edit', params);
    if (res.ok) {
        return Promise.resolve({
            data: res.data,
            success: true,
        });
    }
    return Promise.resolve({
        data: res.data,
        success: false,
    });
}


// 滚调单据
export async function rollBillsOne(whereFrom: any, params: {
    dbname: string;
    orgBillID: any;
    billID: any;
    trueName: string | undefined;
    userCode: string
}) {

    let requestName: any = '';
    if (whereFrom === 1 || whereFrom === '账表调整') {
        requestName = 'account.ZBAdjust.rollBillOne';
    } else {
        requestName = 'account.auditAdjust.rollBillOne';
    }

    const res = await getApiData(requestName, params);
    return Promise.resolve({
        data: res.data,
        success: res.ok,
    });
}


